/**
 * Created by EEO on 2017/6/28.
 */
~function (pro) {
    //->format:解析一个URL地址问号传递参数的值
    function format() {
        var reg = /([^?=&#]+)=([^?=&#]+)/g,
            obj = {};
        this.replace(reg, function () {
            obj[arguments[1]] = arguments[2];
        });
        return obj;
    }

    pro.format = format;
}(String.prototype);
(function() {
    var regExp = config.regExp;
    var $password = $('[name=password]');
    var $pwconfirm = $('[name=pwconfirm]');

    var KEY_ALL = 0;
    var KEY_PHONE = 1;
    var KEY_EMAIL = 2;
    function showCheck($dom, checkInfo) {
        if(!checkInfo.result) {
            $dom.next('.help-block').show().text(checkInfo.text)
        }
        return checkInfo.result
    }
    function checkPassword() {
        var checkInfo = {
            result: true
        };
        if(!regExp.password.test($password.trimVal()) || $password.trimVal() === '') {
            checkInfo.result = false;
            checkInfo.text = '请输入6-20位密码'
        }
        return checkInfo
    }
    function checkConfirm() {
        var checkInfo = {
            result: true
        };
        if(!checkPassword().result) {
            return checkInfo
        }

        if( !($('[name=password]').val() === $('[name=pwconfirm]').val()) ) {
            checkInfo.result = false;
            checkInfo.text = '两次输入密码不一致'
        }
        return checkInfo
    }
    $('.uk-form-row input').on('focus', function () {
        $(this).next('.help-block').hide()
    });
    $password.on('blur', function () {
        var checkInfo = checkPassword();
        if(showCheck($(this),checkInfo)) {
        }
    });
    $pwconfirm.on('blur', function () {
        var checkInfo = checkConfirm();
        if(showCheck($(this),checkInfo)) {
        }
    });

    $('#reset_ok').on('click', function () {
        if(!showCheck($password, checkPassword())) {
            return
        }
        if(!showCheck($pwconfirm, checkConfirm())) {
            return
        }
        var urlObj = window.location.href.format(),
            userid = urlObj['id'];
        $('.login-header').hide();
        $('.uk-form').hide();
        $('.header').show();
        $.ajax({
            url: '/api/user/password/reset',
            method: 'post',
            data: {
                userid:userid,
                password: $password.val(),
                password_confirmation: $pwconfirm.val()
            },
            success: function (res) {
                setTimeout(function () {
                    window.location.href='login.html';
                },1000);
            }

        })
    })


}());
